from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

f = open('si2.txt')
s = f.readlines()
f.close()
hanMap = {}
chMap = {}
co = 0
for one in s:
    co +=1
    aa = one.split('#')
    ab = aa[2].strip().replace('\t','').replace('\n','').replace('\r','')
    try:
        ac = aa[3].strip().replace('\t','').replace('\n','').replace('\r','').replace('序','')
    except Exception as e:
        print (e)
        continue
    han = ab[0]
    ch = ab[1:]
    xu = ac
    if ch not in chMap:
        chMap[ch]=[[han,co,xu]]
    else:
        chMap[ch].append([han,co,xu])
 
keys = chMap.keys()    
class myLine(QLineEdit):
    def __init__(self):
        super().__init__()
        
    def keyPressEvent(self, e):
        if e.key()==Qt.Key_Backspace:
            if not self.text():
                w.mem.setText(w.mem.toPlainText()[:-2]+'|')
        super().keyPressEvent(e)

class myw(QWidget):
    def __init__(self):
        super().__init__()
        vbox = QVBoxLayout()
        self.la = la = QLabel()
        la.setFixedHeight(40)
        la.setStyleSheet("font: 14pt;")
        vbox.addWidget(la)
        self.line = la = myLine()
        f = la.font()
        f.setPointSize(14) 
        la.setFont(f)        
        
        la.textChanged.connect(self.textCh)
        vbox.addWidget(la)   
        self.mem = la = QTextEdit()
        f = la.font()
        f.setPointSize(14) 
        la.setFont(f)
        vbox.addWidget(la)
        self.setLayout(vbox)    
        
        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')    
        self.ca = []
        
    def textCh(self):
        c = self.line.text()
        if len(c)==5 and c[-1].isalpha() and  self.ca:
            self.mem.setText(self.mem.toPlainText()[:-1]+self.ca[0]+'|')
            c = c[-1]
            self.line.setText(c)
            
        if  len(c)==1 and not c.isalpha():
            self.mem.setText(self.mem.toPlainText()[:-1]+c+'|')
            self.line.setText('')
            return            
        if not c:
            return
        newC = c[-1]
        if newC == ' ':
            newC = '1'
        if newC == ',':
            newC = '2'
        if newC == '.':
            newC = '3'
        
        if newC in ['1','2','3','4','5','6']:
            i = int(newC)
            if len(self.ca)>=i:
                self.mem.setText(self.mem.toPlainText()[:-1]+self.ca[i-1]+'|')
                self.line.setText('')
                return
            
        ret = chMap.get(c)
        if not ret:
            ret = []
        chGot = []
        for one in ret:
            chGot.append(one[0])
        if len(ret)<3 and len(c)>=2:
            for one in keys:
                if len(chGot)>=3:
                    break
                if one.startswith(c):
                    ret2 = chMap[one]
                    for bb in ret2:
                        if len(chGot)>=3:
                            break     
                    if bb[0] not in chGot :
                        chGot.append(bb[0])
        newT = ''
        co = 1
        self.ca = chGot
        for one in chGot:
            newT += str(co)+'.'+one +'  '
            co += 1
           
        self.la.setText(newT)
        
a = QApplication([])        
w = myw()
w.show()
a.exec_()